package com.bootdo.provider.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.provider.domain.DriverDO;
import com.bootdo.provider.service.DriverService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 司机表
 * 
 * @author clq
 * @date 2018-05-08 11:53:01
 */
 
@Controller
@RequestMapping("/provider/driver")
public class DriverController {
	@Autowired
	private DriverService driverService;
	
	@GetMapping()
	@RequiresPermissions("provider:driver:driver")
	String Driver(){
	    return "provider/driver/driver";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("provider:driver:driver")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<DriverDO> driverList = driverService.list(query);
		int total = driverService.count(query);
		PageUtils pageUtils = new PageUtils(driverList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "provider/driver/add";
	}

	@GetMapping("/edit/{driverId}")
	@RequiresPermissions("provider:driver:edit")
	String edit(@PathVariable("driverId") Long driverId,Model model){
		DriverDO driver = driverService.get(driverId);
		model.addAttribute("driver", driver);
	    return "provider/driver/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( DriverDO driver){
		if(driverService.save(driver)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( DriverDO driver){
		driverService.update(driver);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long driverId){
		if(driverService.remove(driverId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] driverIds){
		driverService.batchRemove(driverIds);
		return R.ok();
	}
	
}
